
import React, { memo, useState } from 'react'
import { Link } from "react-router-dom"
import { message } from 'antd';
import { vil } from "./verification"

import ParticlesBg from 'particles-bg'

import { setuserdataActions } from "@/store/actionCreators.js"
import { useDispatch } from "react-redux"

import {
    userLogin
} from "api"

export default memo(function Login(props) {
    const [password, setPassword] = useState("")
    const [xuehao, setXuehao] = useState("")

    const dispatch = useDispatch()

    // 提交注册信息
    const submit = async (e) => {
        e.preventDefault()
        // console.log(email,password,password1,xuehao,code_val)
        // 先进行表单验证
        let isTrue = vil(xuehao, password)
        // console.log(isTrue)
        if (isTrue === true) {
            let data = await userLogin({ password, xuehao })
            // console.log(data)
            message.success(data.msg);
            if (data.code - 0 === 200) {
                sessionStorage.setItem("userdata", JSON.stringify(data.data))
                sessionStorage.setItem("token", data.token)
                dispatch(setuserdataActions(data.data))
                // 

                sessionStorage.setItem("base", "http://139.155.253.86:5000/")


                setTimeout(() => {
                    props.history.push("/home/workform")
                }, 500)
            }

        }

    }


    return (
    <>
   <ParticlesBg type="circle" bg={true} />
        <div className="register login">
            <form>
                <h2>
                    LOGIN
                </h2>

                <div className="item">
                    <p>
                        xuehao
                    </p>
                    <input onChange={e => setXuehao(e.target.value.trim())} value={xuehao} id="xuehao" type="text" />
                </div>
                <div className="item">
                    <p>
                        password
                    </p>
                    <input placeholder="密码长度 4 -20 个字符" onChange={e => setPassword(e.target.value.trim())} value={password} id="password" type="password" />
                </div>
                <div className="footer_item">
                    <button onClick={submit}>
                        submit
                    </button>
                </div>
                <div className="footer_item">
                    <Link to="/register">
                        register
                    </Link>
                    <Link to="/findpassword">
                        findpassword
                    </Link>
                </div>
            </form>

        </div>
        </>
    )
})

